package yxy.game.pm2.active.module;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actcontrol;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.bean.timeline.TimelineModule;

import java.util.ArrayList;
import java.util.Collection;

public class ServerActiveInfo extends TimelineModule {

	private String areaid;
	private String acttype; // 活动id
	private String name;
	private DateTime starttime; // 活动开启时间
	private DateTime endtime; // 活动结束时间
	private int opentype; // 0.按照开始时间 1.按照开服时间
	private int circledays; // 循环间隔天数
	private String reward; // 奖励
	private int operationcycle;	//运行时间
	private ArrayList<Integer> groups = new ArrayList<Integer>();	//组

	public String getAreaid() {
		return areaid;
	}
	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}
	public String getActtype() {
		return acttype;
	}
	public void setActtype(String acttype) {
		this.acttype = acttype;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public DateTime getStarttime() {
		return starttime;
	}
	public void setStarttime(DateTime starttime) {
		this.starttime = starttime;
	}
	public DateTime getEndtime() {
		return endtime;
	}
	public void setEndtime(DateTime endtime) {
		this.endtime = endtime;
	}
	public int getOpentype() {
		return opentype;
	}
	public void setOpentype(int opentype) {
		this.opentype = opentype;
	}
	public int getCircledays() {
		return circledays;
	}
	public void setCircledays(int circledays) {
		this.circledays = circledays;
	}
	public String getReward() {
		return reward;
	}
	public void setReward(String reward) {
		this.reward = reward;
	}
	public int getOperationcycle() {
		return operationcycle;
	}
	public void setOperationcycle(int operationcycle) {
		this.operationcycle = operationcycle;
	}
	public ArrayList<Integer> getGroups() {
		return groups;
	}
	public void setGroups(ArrayList<Integer> groups) {
		this.groups = groups;
	}
	
	// info===========================================================================


	// 某个服某个活动是否开启
	public boolean isRuning() {
	
//		// 起始时间未设定
//		DateTime start = getStarttime();
//		DateTime end = getEndtime();
//
//		DateTime now = DateTime.now();
//
//		if (start == null && end == null) {
//			return true;
//		}
//		if (start == null && end.isBefore(now)) {
//			return true;
//		}
//		if (end == null) {
//			return true;
//		}
//		if (now.isBefore(end)) {
//			return true;
//		}
//		return false;
		
		return running();
	}

	/**
	 * 获取某个服某个活动信息
	 * 
	 * @param sid
	 * @param actid
	 * @return
	 */
	public static ServerActiveInfo getAreaOneActionInfo(String areaid, String acttype) {
		ServerActiveInfo sai = get(areaid, acttype);
		return sai;
	}
	
	public static ServerActiveInfo getAreaOneActionInfo(int areaid, String acttype) {
		ServerActiveInfo sai = get(String.valueOf(areaid), acttype);
		return sai;
	}

	public static ServerActiveInfo create(int areaid, actcontrol data) {
		ServerActiveInfo sai = new ServerActiveInfo();
		
		int acttype = data.getId();

		DateTimeFormatter datetimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd#HH:mm:ss");
		DateTime start = DateTime.parse(data.getBegintime(), datetimeFormat);
		DateTime end = DateTime.parse(data.getEndtime(), datetimeFormat);

		sai.setAreaid(String.valueOf(areaid));
		sai.setActtype(String.valueOf(acttype));
		sai.setName(data.getName());
		sai.setCircledays(data.getCycle());
		sai.setEndtime(end);
		sai.setOpentype(data.getOpentype());
		sai.setReward(""); // AREA:%s:%s
		sai.setStarttime(start);

		int cycle = data.getCycle();
		long circletime = 0;
		if (cycle == -1) {
			circletime = end.getMillis() - start.getMillis();
		} else {
			circletime = cycle * ActiveUtil.DAY;
		}

		Timeline tl = new Timeline();
		tl.setStartTime(start);
		tl.setEndTime(end);
		tl.setCircleTime(circletime);
		tl.setRound(0);
		
		sai.setTimeline(tl);
		sai.set();
		return sai;
	}
	
	/**
	 * 获取某个服所有活动信息
	 * 
	 * @param sid
	 */
	public JSONObject getAreaActionInfo(String areaid, String playercode) {
		JSONObject actsinfo = new JSONObject();

		Iterable<String> keys = redis().client().getKeys().getKeysByPattern(key(areaid, "*"));
		for (String key : keys) {
			String[] ks = key.split(":");
			String actid = ks[ks.length - 1];
			ServerActiveInfo sai = get(key);
			if (sai.isRuning()) {
				actsinfo.put(actid, true);
			}
		}
		return actsinfo;
	}

	// redis==========================================================================

	private static String key(String areaid, String acttype) {
		return String.format("AREA:%s:ACTTYPE:%s", areaid, acttype);
	}

	public void set() {
		try {
			setv(key(getAreaid(), getActtype()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public void set(String key) {
		try {
			setv(key, this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ServerActiveInfo get(String areaid, String acttype) {
		try {
			ServerActiveInfo module = getv(key(areaid, acttype));
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public ServerActiveInfo get(int areaid, int acttype) {
		return get(String.valueOf(areaid), String.valueOf(acttype));
	}
	
	static public ServerActiveInfo get(String key) {
		try {
			ServerActiveInfo module = getv(key);
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/** 正常情况下不要使用这个方法 */
	static public Collection<ServerActiveInfo> all(int areaid) {
		ArrayList<ServerActiveInfo> list = new ArrayList<>();
		try {
			Iterable<String> iterable = redis().client().getKeys().getKeysByPattern(key(String.valueOf(areaid), "*"));
			for (String code : iterable) {
				ServerActiveInfo bean = getv(code);
				if (bean != null) {
					list.add(bean);
				}
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return list;
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getAreaid(), getActtype()));
	}

}
